#include "lexer.h"
#include "error.h"
#include "table.h"
#include <string>
#include <iostream> 
using namespace std;
using namespace Error;
using namespace Table;
using namespace Lexer;

Token_value Lexer::curr_tok = PRINT;
double Lexer::number_value=0;
string Lexer::string_value;

Token_value Lexer::get_token(istream& in)
{
	char ch = 0;
	in.get(ch);      
	while (ch != '\n' && isspace(ch)) {  
		if (!in.get(ch)) return curr_tok = END;   
	}

	switch (ch) {
	case 0:  return END;
	case ';':
	case EOF:
	case '\n': return curr_tok = PRINT;
	case '*':
	case '/':
	case '+':
	case '-':
	case '(':
	case ')':
	case '=': return curr_tok = Token_value(ch);
	case '0': case '1': case '2':case '3':case '4':
	case '5': case '6': case '7':case '8':case '9':
	case '.': { //NUMBER
		in.putback(ch);
		in >> number_value;
		return curr_tok = NUMBER;
	}
	default: {  

		if (isalpha(ch)) {
			string_value = ch;
			while (in.get(ch) && isalnum(ch))
				string_value.push_back(ch);
			in.putback(ch);
			return curr_tok = NAME;
		}
		Error::error("bad token");
		return curr_tok = PRINT;
	}
	}
}